/*
 * @Description: 创建文件读取流，可控制读取过程
 * @Author: dh
 * @Date: 2021-09-02 16:51:50
 * @LastEditors: dh
 * @LastEditTime: 2023-10-24 14:56:39
 */

const fs = require('fs')
const path = require('path')

//创建可读流,设置读取范围和一次读取多少个字节
const reader = fs.createReadStream(path.join(__dirname, './test.txt'), {
	start: 0,
	end: 10,
	highWaterMark: 3, // 每次读取字节数
})

reader.on('data', data => {
	console.log(data.toString('utf-8'))
	console.log(`到目前为止已读取${reader.bytesRead}个字节`)
	//暂停读取
	reader.pause()
	//设置过2秒钟读取一次数据
	setTimeout(() => {
		//恢复读取
		reader.resume()
	}, 1000)
})
reader.on('open', () => {
	console.log('文件已打开', reader.pending)
})
reader.on('ready', () => {
	console.log('数据已准备好读取', reader.pending)
})
reader.on('close', () => {
	console.log('数据已读完')
})
